package com.thy.axq.util;

import java.text.SimpleDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Json格式处理�?
 */
public final class JsonUtil {
	private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);
	private static ObjectMapper objectMapper = new ObjectMapper();
	
	static {
		objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
	}
	

	/** 私有构�?�?**/
	private JsonUtil() {
	}
	/**
	 * 将Object对象转换成Json
	 * 
	 * @param object
	 *            Object对象
	 * @return Json字符�?
	 */
	public static String convertObject2Json(Object object) {
		String method = "convertObject2Json";
		try {
			return objectMapper.writeValueAsString(object);
		} catch (Exception e) {
			logger.error("convertObject2Json",method, e);
		}
		return null;
	}

	/**
	 * 将Json转换成Object对象
	 * 
	 * @param json
	 *            Json字符�?
	 * @param cls
	 *            转换成的对象类型
	 * @return 转换之后的对�?
	 */
	public static <T> T convertJson2Object(String json, Class<T> cls) {
		String method = "convertJson2Object";
		try {
			return objectMapper.readValue(json, cls);
		} catch (Exception e) {
			logger.error(method, "convert json error.", json, e);
		}
		return null;
	}
}
